package com.global.college.api;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.global.college.CollegeApplication;
import com.global.college.R;
import com.global.college.UserData;
import com.global.college.ui.activity.LoginByPswActivity;
import com.global.college.utils.NetworkUtils;
import com.global.college.utils.ToastUtils;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

import retrofit2.adapter.rxjava.HttpException;
import rx.Subscriber;

public abstract class ProgressSubscriber<T> extends Subscriber<T> implements ProgressDialogListener {

    /**
     * 上下文
     */
    private Context context;

    private ProgressDialogHandler mProgressDialogHandler;

    private boolean showDialog = true;//dialog被cancel掉的时候，是否取消掉事件监听

    //通过构造方法传递参数是否显示和隐藏显示框
    public ProgressSubscriber(Context context) {
        this.context = context;
        this.mProgressDialogHandler = new ProgressDialogHandler(false, this, context);
    }

    public ProgressSubscriber(Context context, boolean showDialog) {
        this.context = context;
        this.mProgressDialogHandler = new ProgressDialogHandler(false, this, context);
        this.showDialog = showDialog;
    }

    private void showProgressDialog() {
        if (mProgressDialogHandler != null) {
            mProgressDialogHandler.obtainMessage(ProgressDialogHandler.SHOW_DIALOG).sendToTarget();
        }
    }

    private void dismissProgressDialog() {
        if (mProgressDialogHandler != null) {
            mProgressDialogHandler.obtainMessage(ProgressDialogHandler.DISMISS_DIALOG).sendToTarget();
            mProgressDialogHandler = null;
        }
    }

    @Override
    public void onStart() {
        if (!NetworkUtils.isNetworkAvailable(context)) {
            ToastUtils.showToastShort(context.getString(R.string.network_error_please_check));
            if (!this.isUnsubscribed()) {
                this.unsubscribe();
            }
            onFinish();
            return;
        }
        if (showDialog) {
            showProgressDialog();
        }
    }

    @Override
    public void onCompleted() {
        dismissProgressDialog();
        onFinish();
        Log.e("onCompleted()-----","onCompleted()");
    }

    @Override
    public void onError(Throwable e) {
        Log.e("onError-----",e.getMessage());
        e.printStackTrace();
        if (e instanceof SocketTimeoutException) {
            ToastUtils.showToastShort(context.getString(R.string.network_error_please_check));
        } else if (e instanceof ConnectException) {
            ToastUtils.showToastShort(context.getString(R.string.network_error_please_check));
        } else if (e instanceof ApiException) {
            ApiException ae = (ApiException) e;
            String type = ae.code;
            if ("-200".equals(type)) {
            }else {
                ToastUtils.showToastShort(ae.getMessage());
            }
        } else if(e instanceof HttpException){
            UserData.getInstance().ClearAllCachData();
            CollegeApplication.getContext().getApplicationContext().startActivity(new Intent(CollegeApplication.getContext().getApplicationContext(), LoginByPswActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
            ToastUtils.showCenterToast("用户信息失效，请重新登录");
        }else {
            ToastUtils.showToastShort(e.getMessage());
        }
        dismissProgressDialog();
        onFinish();
    }

    @Override
    public void onCancelProgress() {
        Log.e("onCancelProgress()-----","onCancelProgress()");
        if (!this.isUnsubscribed()) {
            this.unsubscribe();
        }
    }

    public void onFinish() {
        Log.e("onFinish()-----","onFinish()");
        //根据具体业务场景重写该方法以实现自己的需求

    }

}
